package com.hzhc.controller.baseinfo;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.YytNotice;
import com.hzhc.entity.ZcFile;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.YytNoticeService;
import com.hzhc.service.ZcFileService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.YytNoticeVo;
import com.hzhc.vo.ZcFileVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 政策文件
 * @author zhoumx
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/zcFile")
public class ZcUploadController extends BaseController {
    private final ZcFileService zcFileService;
    @Value("${zcFile}")
    private String zcFilefolder;

    @Value("${file.zcUploadFolder}")
    private String zcUploadFolder;
    @Value("${file.zcAccessPath}")
    private String zcAccessPath;
    /**
     * 分页列表
     */
    @RequestMapping("/getZcFileVoList")
    public ResultUtil<List<ZcFileVo>> getYyNoticeVoList(@RequestBody ZcFileVo zcFileVo){
        Page<ZcFileVo> page=new Page<>(zcFileVo.getPageIndex(),zcFileVo.getPageSize());
        List<ZcFileVo> list=zcFileService.getZcFileList(page,zcFileVo);
        return ResultUtil.build(list, page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }



    /**
     * 删除
     */
    @PostMapping("/delZcFile/{id}")
    @SysTemLog(operModul = "政策文件-删除", operType = "删除", operDesc = "政策文件删除功能", modular = "PC")
    public ResultUtil<T> delYyNotice(@PathVariable("id") Long id){
        Boolean bool = zcFileService.delYytNotice(id);
        return bool ? ResultUtil.ok() : ResultUtil.fail();
    }
    /**
     * 上传政策文件
     */
    @PostMapping(path = "/uploadZcFile", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultUtil<String> uploadZhengceFile(HttpServletRequest request,ZcFile zcFile,
                                         @RequestParam(value = "file", required = false) MultipartFile file) {
        UserDto user= TokenUtil.getUserMember(request);
        zcFile.setCreateUser(user.getRealname());
        zcFile.setCreateTime(LocalDateTime.now());
        return zcFileService.upZcfile(zcFile,file, zcUploadFolder, zcAccessPath);
    }
    /**
     * 保存
     */
    @RequestMapping("/saveZcFile")
    public ResultUtil<List<ZcFileVo>> saveFile(HttpServletRequest request,@RequestBody ZcFile zcFile){
        UserDto user= TokenUtil.getUserMember(request);
        zcFile.setCreateUser(user.getRealname());
        zcFile.setCreateTime(LocalDateTime.now());
        boolean b= zcFileService.save(zcFile);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

}





